/**
 * 
 */
package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.CustomerType;
import com.jxc.model.po.Repertory;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VCustomerType;
import com.jxc.service.CustomerTypeServiceI;

/**
 * @author Tino
 * 
 */
@Service("customerTypeService")
public class CustomerTypeServiceImpl implements CustomerTypeServiceI {

	@Autowired
	private BaseDaoI<CustomerType> customerTypeDao;

	@Override
	public List<VCustomerType> combobox(VCustomerType vct) {
		return this.changeModel(this.find(vct));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.CustomerTypeServiceI#datagrid(com.jxc.model.vo.VCustomerType)
	 */
	@Override
	public DataGrid datagrid(VCustomerType vct) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vct)));
		dg.setTotal(this.total(vct));
		return dg;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.CustomerTypeServiceI#delete(java.lang.String)
	 */
	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					CustomerType ct = customerTypeDao.get(CustomerType.class, Long.parseLong(id.trim()));
					if (ct != null) {
						customerTypeDao.delete(ct);
					}
				}
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.CustomerTypeServiceI#save(com.jxc.model.vo.VCustomerType)
	 */
	@Override
	public void save(VCustomerType vct) {
		CustomerType ct = new CustomerType();
		BeanUtils.copyProperties(vct, ct);
		if (vct.getRid() != null) {
			ct.setRepertory(new Repertory(vct.getRid()));
		}
		this.customerTypeDao.save(ct);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jxc.service.CustomerTypeServiceI#update(com.jxc.model.vo.VCustomerType)
	 */
	@Override
	public void update(VCustomerType vct) {
		CustomerType ct = customerTypeDao.get(CustomerType.class, vct.getId());
		BeanUtils.copyProperties(vct, ct, new String[] { "id" });
		if (vct.getRid() != null) {
			ct.setRepertory(new Repertory(vct.getRid()));
		}
		this.customerTypeDao.update(ct);
	}

	private List<CustomerType> find(VCustomerType vct) {
		String hql = "from CustomerType t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vct, hql, values);

		if (vct.getSort() != null && vct.getOrder() != null) {
			hql += " order by " + vct.getSort() + " " + vct.getOrder();
		}
		return customerTypeDao.find(hql, values, vct.getPage(), vct.getRows());
	}

	private List<VCustomerType> changeModel(List<CustomerType> cts) {
		List<VCustomerType> vcts = new ArrayList<VCustomerType>();
		if (cts != null && cts.size() > 0) {
			for (CustomerType ct : cts) {
				VCustomerType vct = new VCustomerType();
				BeanUtils.copyProperties(ct, vct);
				if (ct.getRepertory() != null) {
					Repertory repertory = ct.getRepertory();
					vct.setRid(repertory.getId());
					vct.setRname(repertory.getName());
				}
				vcts.add(vct);
			}
		}
		return vcts;
	}

	private Long total(VCustomerType vct) {
		String hql = "select count(*) from CustomerType t where 1=1  ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vct, hql, values);
		return customerTypeDao.count(hql, values);
	}

	private String addWhere(VCustomerType vct, String hql, List<Object> values) {
		if (vct.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vct.getId());
		}
		if (vct.getRid() != null) {
			hql += " and t.repertory.id = ? ";
			values.add(vct.getRid());
		}
		return hql;
	}
}
